/*----------------------------------------------------------------------------------------
*
*  Copyright 2019, Gao Hai Hui, <fromzeropoint@126.com>.  All rights reserved.
*  https://gitee.com/helloworldghh/xoskit.git
*  Use of this source code is governed by a MIT license
*  that can be found in the License file.
*
----------------------------------------------------------------------------------------*/
#ifndef __3EB4BC82_5991_49FC_8BC9_14960568CFCF__
#define __3EB4BC82_5991_49FC_8BC9_14960568CFCF__

#if defined( XOS_MAC ) || defined( XOS_IOS_SIMULATOR ) || defined( XOS_IOS_STATIC )
    #include "xos_helper_mac.h"
#elif defined( XOS_LINUX ) || defined( XOS_ANDROID )
    #include "xos_helper_linux.h"
#elif defined( XOS_X64 )
    #include "xos_helper_win.h"
#endif

namespace xos_helper
{
    // 
    // combine load and get_proc_by_name.
    // 
    // 1.try to load : lpszPath/lpszFileName.xos first, if failed, then try : lpszPath/lpszSearchPath/lpszFileName.xos
    // 
    // lpszPath : like c:\\windows or /opt/test
    // lpszFileName : like "mydll", without ext
    // lpszProcName : exported function name
    // lpszSearchPath : like "xos"
    // 
    static int xos_load_and_get( void ** ppv, void* & pModule, const char * lpszPath, const char * lpszFileName, const char * lpszProcName )
    {
        void * pRet = 0;
        int ret = 0;

        char szPath[4096] = { 0 };

        if( 0 == ret )
        {
            if( !lpszPath )
            {
                xos_get_process_path( szPath, sizeof( szPath ) / sizeof( szPath[0] ), 0 );
            }
            else
            {
                xos_strcpy( szPath, lpszPath );
            }
        }

        if( 0 == ret )
        {
            if( 0 == ret )
            {
                char file[4096] = { 0 };
                xos_strcpy( file, szPath );
                xos_path_append( file, lpszFileName );
                xos_strcat( file, ".xos" );
                ret = xos_load( file, pModule );
            }
        }

        if( 0 == ret )
        {
            ret = xos_get_proc_by_name( &pRet, pModule, lpszProcName );
        }

        if( 0 == ret )
        {
            *ppv = pRet;
            pRet = 0;
        }

        if( ( 0 != ret ) && pModule )
        {
            xos_free( pModule );
            pModule = 0;
        }

        return ret;
    }

} // xos_helper

#endif // __3EB4BC82_5991_49FC_8BC9_14960568CFCF__
